﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.DataAccess.DataInf;
using FB.Utilities;

namespace FB.DataAccess
{
    public class ChanelDAO : IChanelDAO
    {
        private static FBDATA dataContext;
        
        private IRepository<Chanel> repository;
        public ChanelDAO()
        {
            dataContext = new FBDATA();
            repository = new RepositoryDAO<Chanel>(dataContext);
        }

        public IEnumerable<Chanel> GetAllChanel()
        {
            return repository.GetQuery().OrderByDescending(aobj => aobj.CreatedDate);
        }
        public Chanel GetChanelByID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();
        }
        public Chanel GetLastRecord()
        {
            return dataContext.Chanels.OrderByDescending(obj => obj.ID).FirstOrDefault();
        }

        public bool UpdateChanel(Chanel inChanel)
        {
            if (inChanel == null) return false;
            Chanel upChanel = repository.First(sobj => sobj.ID == inChanel.ID);
            if (upChanel == null) return false;
            upChanel.Name = inChanel.Name;
            upChanel.DisplayName = inChanel.DisplayName;
            upChanel.CategoryID = inChanel.CategoryID;
            upChanel.Enable = inChanel.Enable;
            upChanel.CreatedDate = inChanel.CreatedDate;
            upChanel.LastFeedDate = inChanel.LastFeedDate;
            upChanel.FeedIndex = inChanel.FeedIndex;
            return repository.SaveChanges() > 0;
        }
        public bool InsertChanel(Chanel inChanel)
        {
            if (inChanel == null) return false;
            if (IsExist(inChanel.Name)) return false;
            repository.Add(inChanel);
            return repository.SaveChanges() > 0;
        }

        public bool IsExist(string chanelName)
        {
            return repository.Exist(chanel => chanel.Name == chanelName);
        }

        public ReturnValue<bool> DelChanel(Guid id)
        {
            if (id == Guid.Parse("ac5ddc8e-cb1e-4ae2-83e0-4dfe32ec6d16")) return new ReturnValue<bool>(false, "You cant delete this chanel");

            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                Chanel objDel = repository.First(obj => obj.ID == id);
                ret = DelChanel(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelChanel(Chanel inChanel)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inChanel == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inChanel);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        /// <summary>
        /// Get a chanel which have last feed date more than 15 day before
        /// </summary>
        /// <returns></returns>
        public Chanel GetNeed2FeedChanel()
        {
            DateTime CountDate = DateTime.Today.AddDays(0);
            var q = (from m in dataContext.Chanels
                     where m.LastFeedDate < CountDate
                     orderby Guid.NewGuid()
                     select m ).FirstOrDefault();
            return q;
        }
    }
}
